package com.glsc.ngateway.oaflow.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.glsc.ngateway.common.api.common.dto.PageDto;
import com.glsc.ngateway.common.api.common.dto.PageQueryParamDto;
import com.glsc.ngateway.common.api.oaflow.resp.OaflowResponse;
import com.glsc.ngateway.common.api.oaflow.dto.xirtrade.*;
import com.glsc.ngateway.oaflow.service.xirtrade.TOaFlowXirTradeService;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author xuchang
 */
@RestController
@RequestMapping("/oaflow/xir")
public class OaFlowXirTradeInfoController {
    private static final Logger logger = LoggerFactory.getLogger(OaFlowXirTradeInfoController.class);

    @Autowired
    private TOaFlowXirTradeService xirTradeService;

    /**
     * 查询
     */
    @PostMapping("/search")
    public OaflowResponse<PageDto<TOaFlowXirTradeBaseInfoDto>> query(@RequestBody PageQueryParamDto<TOaFlowXirTradeInfoSearchParamDto> queryParam) {
        if(CollectionUtil.isEmpty(queryParam.getSorter()) || Strings.isBlank(queryParam.getSorter().get(0).getField())){
            queryParam.setSorter(List.of(new PageQueryParamDto.Sort("id", PageQueryParamDto.Sort.ORDER_DESC)));
        }
        return OaflowResponse.successData(xirTradeService.query(queryParam));
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public OaflowResponse<Void> delete(@RequestBody TOaFlowXirTradeInfoDeleteParamDto param) {
        if(Objects.isNull(param.getId())){
            return OaflowResponse.failMsg("缺少参数");
        }
        xirTradeService.delete(param.getId());
        return OaflowResponse.successMsg("删除成功");
    }

    /**
     * 查询配置
     */
    @PostMapping("/config/search")
    public OaflowResponse<PageDto<TOaFlowXirTradeConfigDto>> searchConfig(@RequestBody PageQueryParamDto<TOaFlowXirTradeConfigSearchParamDto> param) {
        if(CollectionUtil.isEmpty(param.getSorter()) || Strings.isBlank(param.getSorter().get(0).getField())){
            param.setSorter(List.of(new PageQueryParamDto.Sort("id", PageQueryParamDto.Sort.ORDER_DESC)));
        }
        return OaflowResponse.successData(xirTradeService.queryConfig(param));
    }
    /**
     * 删除配置
     */
    @PostMapping("/config/delete")
    public OaflowResponse<Void> deleteConfig(@RequestBody TOaFlowXirTradeConfigDto param) {
        if(Objects.isNull(param.getId())){
            return OaflowResponse.failMsg("缺少参数");
        }
        xirTradeService.deleteConfig(param.getId());
        return OaflowResponse.successMsg("删除成功");
    }
    /**
     * 添加或修改配置
     */
    @PostMapping("/config/addOrUpdate")
    public OaflowResponse<Void> addOrUpdateConfig(@RequestBody TOaFlowXirTradeConfigDto param) {
        xirTradeService.addOrUpdateConfig(param);
        return OaflowResponse.successMsg("添加成功");
    }
}
